

public class Test1 {

    public static SparseMatrix addSMatrix(SparseMatrix a,SparseMatrix b){
        //计算两个三元组表示的稀疏矩阵之和
        if(a.rows!=b.rows ||a.cols!=b.cols){
            System.out.println("这两个矩阵不能相加");
            return null;
        }
        SparseMatrix c = new SparseMatrix(a.nums+b.nums);
        int i=0,j=0,k=0 ;
        while(i<a.nums&&j<b.nums){
            
            if(a.data[i].row<b.data[j].row){   // A行<B行
                c.data[k].column = a.data[i].column;
                c.data[k].row = a.data[i].row;
                c.data[k].value = a.data[i].value+b.data[j].value;
                c.nums = ++k;
                i++;
            }else if(a.data[i].row == b.data[j].row){  // A行=B行
                if(a.data[i].column==b.data[j].column){ //A列 = B列
                    if(a.data[i].value + b.data[j].value != 0){
                        c.data[k].column = a.data[i].column;
                        c.data[k].row = a.data[i].row;
                        c.data[k].value = a.data[i].value+b.data[j].value;
                        System.out.println( c.data[k].value);
                        c.nums = k++;
                    }
                    i++;
                    j++;
                }else if(a.data[i].column <b.data[j].column){ //A列<B列
                    c.data[k].column = a.data[i].column;
                    c.data[k].row = a.data[i].row;
                    c.data[k].value = a.data[i].value;
                    c.nums = ++k;
                    i++;
                }else if(a.data[i].column >b.data[j].column){ //A列>>B列
                    c.data[k].column = b.data[j].column;
                    c.data[k].row = b.data[j].row;
                    c.data[k].value = b.data[j].value;
                    c.nums = ++k;
                    j++;
                }
            }else if(a.data[i].row < b.data[j].row){//A行>B行
                c.data[k].column = b.data[j].column;
                c.data[k].row = b.data[j].row;
                c.data[k].value = b.data[j].value;
                c.nums = ++k;
                j++;
            }
            break;
        }
        
        while(i<a.nums){
            c.data[k].column = a.data[i].column;
            c.data[k].row = a.data[i].row;
            c.data[k].value = a.data[i].value;
            c.nums = ++k;
            i++;
        }
        while(j<b.nums){
            c.data[k].column = b.data[j].column;
            c.data[k].row = b.data[j].row;
            c.data[k].value = b.data[j].value;
            c.nums = ++k;
            j++;
        }
        System.out.println(k);
		return c; 
     }

    public static void main(String[] args) {
        int matrixA[][] = {{3,0,0,7},{0,0,-1,0},{2,0,0,0},{0,0,0,0},{0,0,0,-8}};
        int matrixB[][] = {{-3,0,0,0},{1,0,0,0},{0,3,0,0},{0,2,0,0},{0,0,0,0}};
        SparseMatrix tsm1 = new SparseMatrix(matrixA);
        SparseMatrix tsm2 = new SparseMatrix(matrixB);
        System.out.println("矩阵A；");
        tsm1.printMatrix1();
        tsm1.printMatrix();
        System.out.println("矩阵B:");
        tsm2.printMatrix1();
        tsm2.printMatrix();
        SparseMatrix matrixSum = addSMatrix(tsm1, tsm2);
        System.out.println("矩阵A+矩阵B:");
        matrixSum.printMatrix();
    }
}
